(function () {
	"use strict";
	var $ = {
		query    : function (/*String*/dom, /*Boolean*/on_off = false) {
			if (typeof dom === "string") {
				let t = on_off ? document.querySelectorAll(dom) : document.querySelector(dom);
				return t
			} else {
				throw "传入值非法";
			}
		},
		className: function (dom, name) {
			name    = name || "";
			let len = dom.length;
			if (len > 1) {
				for (let i = 0; i < len; i++) {
					dom[i].className = name;
				}
			} else {
				dom.className = name;
			}
		},
		clone    : function (obj) {
			function Clone() {
			}

			Clone.prototype = obj;
			let o           = new Clone();
			for (let i in o) {
				if (typeof o[i] == "object") {
					o[i] = this.clone(o[i]);
				}
			}
			return o;
		},
		slider   : {
			// 判断设备是否支持touch
			touch  : ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch,
			// 全局对象
			id     : "#slider",
			setId  : function (/*String*/id) {
				this.id = id;
			},
			getId  : function () {
				return this.id;
			},
			icon   : ".icon",
			setIcon: function (/*String*/icon) {
				this.icon = icon;
			},
			getIcon: function () {
				return this.icon;
			},
			// 执行的方法
			fnMoveT: new Function,
			fnMoveB: new Function,
			fnMoveL: new Function,
			fnMoveR: new Function,
			fnEndT : new Function,
			fnEndB : new Function,
			fnEndL : new Function,
			fnEndR : new Function,
			// 事件
			events : {
				let        : {
					isScrollXY   : 0, // 是否横向滑动
					isScrollLR   : 0, // 判断左右
					isScrollTB   : 0, // 判断上下
					startLocation: null, // 开始的位置
					endLocation  : null  // 最终位置
				},
				// 事件绑定
				handleEvent: function (event) {
					if (event.type === 'touchstart') {
						this.start(event);
					} else if (event.type === 'touchmove') {
						this.move(event);
					} else if (event.type === 'touchend') {
						this.end(event);
					}
				},
				// 滑动状态
				start      : function (event) {
					// 取得数组对象中第一 Touch
					let touch              = event.targetTouches[0];
					// 取开始坐标
					this.let.startLocation = {x: touch.pageX, y: touch.pageY, time: +new Date};
					this.events.dom.addEventListener('touchmove', this, false);
					this.events.dom.addEventListener('touchend', this, false);
				},
				move       : function (event) {
					// 屏幕出现多个Touch 或页面被缩放则不执行
					if (event.targetTouches.length > 1 || event.scale && event.scale !== 1) {
						return
					}
					let touch            = event.targetTouches[0];
					this.let.endLocation = {
						x: touch.pageX - this.let.startLocation.x,
						y: touch.pageY - this.let.startLocation.y
					};
					this.let.isScrollXY  = Math.abs(this.let.endLocation.x) < Math.abs(this.let.endLocation.y) ? 1 : 0;
					this.let.isScrollLR  = Math.abs(this.let.startLocation.x) < Math.abs(this.let.endLocation.x) ? 1 : 0;
					this.let.isScrollTB  = Math.abs(this.let.startLocation.y) < Math.abs(this.let.endLocation.y) ? 1 : 0;
					event.preventDefault(); //阻止触摸事件的默认行为，即阻止滚屏
					if (this.let.isScrollXY === 0) { //当为水平滚动时
						// doSomething
						if (this.let.isScrollLR === 0) {
							this.events.fnMoveL();
						} else {
							this.events.fnMoveR();
						}
					} else {
						if (this.let.isScrollTB === 0) {
							this.events.fnMoveT();
						} else {
							this.events.fnMoveB();
						}
					}
				},
				end        : function (event) {
					event.preventDefault(); //阻止触摸事件的默认行为，即阻止滚屏
					//let duration = +new Date - this.let.startLocation.time; //滑动的持续时间
					if (this.let.isScrollXY === 0) { //当为水平滚动时
						// doSomething
						if (this.let.isScrollLR === 0) {
							this.events.fnEndL();
						} else {
							this.events.fnEndR();
						}
					} else {
						if (this.let.isScrollTB === 0) {
							this.events.fnEndT();
						} else {
							this.events.fnEndB();
						}
					}
					this.events.dom.removeEventListener('touchmove', this, false);
					this.events.dom.removeEventListener('touchend', this, false);
				}
			},
			//初始化
			init   : function () {
				// slider 指针保留到events 给内部调用
				this.events.events = this;
				this.dom           = $.query(this.id);
				// 全局：初始化节点
				if (!!this.touch) {
					this.dom.addEventListener('touchstart', this.events, false);
				} //addEventListener第二个参数可以传一个对象，会调用该对象的handleEvent属性
			}
		}
	};
})();